package com.java.controller;

import com.alibaba.fastjson.JSONArray;
import com.java.dao.UserStyleMapper;
import com.java.pojo.*;
import com.java.pojo.EnterGood;
import com.java.service.StoresService;
import com.java.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private StoresService storesService;

    @Autowired
    private UserStyleMapper userStyleMapper;


    @RequestMapping("/center")
    public String s(){
        return "util/makerAndenterGoodAndselect";
    }

    @RequestMapping("/login")
    public String l(){
        return "center/center";
    }


    @RequestMapping("/findAllUser")
    public String findAllUser(@RequestParam(value = "start",required = false) Integer start,
                              @RequestParam(value = "username",required = false)String username ,
                              Model model){
        if(username==null ||"".equals(username)){
            username="";
        }
        if(start==null ||start.equals("")){
            start=1;
        }
        System.out.println(start+"+"+username);
        /*计算总页数*/
        int totalCount = userService.getCount(username);
        int i = totalCount % 4 ==0? totalCount/4 :(totalCount/4)+1;
        /*如果最后一页大于总页数,就让其等于总页数,html按钮最后一页
         * 如果前一页到0的时候,把他变成1*/
        if(start>=i){
            start=i;
        }else if(start<=1){
            start=1;
        }
        Page<User> page =userService.getPageByUser(start,username);
        System.out.println("page:"+page);

            model.addAttribute("page",page);
            model.addAttribute("username",username);
            return "user/findAllUser";
    }


    //添加页面
    @RequestMapping("/addUser")
    public String addUser(Model model){
        List<UserStyle> userStyleList = userStyleMapper.getUserStyleList();
        List<Stores> storesList = storesService.getStoresList();
        model.addAttribute("user",userStyleList);
        model.addAttribute("st",storesList);
        return "user/AddUser";
    }

    @RequestMapping(value = "/ADDUSER",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String ADDUSER(User user){
        int i = userService.addUser(user);
        if(i>0){
            return "<script>alert('ADD successful');location.href='/user/findAllUser';</script>";
        }else {
            return "<script>alert('username重复! ADD 失败!');location.href='/user/findAllUser';</script>";
        }
    }

    @RequestMapping(value = "/DELUSER",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String DELUSER(@RequestParam("uid") Integer uid){
        int i = userService.delUser(uid);
        if(i>0){
            return "<script>alert('DEL successful');location.href='/user/findAllUser';</script>";
        } else{
            return "<script>alert('DEL 失败!');location.href='/user/findAllUser';</script>";
        }
    }


    /*ajax*/
    //添加user   json
    @RequestMapping("/ADDUser2")
    @ResponseBody
    public Object log2(@RequestParam("name") String name){
        HashMap<String,String> map=new HashMap<>();//集合
        User byName = userService.getUserByName(name);
        System.out.println(name+":"+byName);
        if(byName!=null){
            map.put("userCode","exist");
        }else{
            map.put("userCode","no");
        }
        //将map转为json
        return JSONArray.toJSONString(map);
    }




}
